import ebooklib
from bs4 import BeautifulSoup
from ebooklib import epub

book = epub.read_epub("C:\\Users\\70745\\Downloads\\次第花开-希阿荣博堪布.epub")

# 解析
i = 0
for item in book.get_items():
    # 提取书中的文本内容

    if item.get_type() == ebooklib.ITEM_DOCUMENT:
        # epub中的内容是html格式，使用BeautifulSoup可以完美解析
        soup = BeautifulSoup(item.get_body_content(), 'lxml')
        # text_str = soup.text
        # text_str=text_str.replace("\n","")
        # if len(text_str)>1:
        #     print(text_str)
        i = i + 1
        if 5 < i < 28:
            flg = True
            f = open(
                f'C:\\Users\\70745\\PycharmProjects\\edgeTtsWrapper\\data'
                f'\\tmp.txt', mode='w')
            for j, child in enumerate(soup.descendants):  # i接受索引，child接受内容
                child_str = str(child)
                child_str = child_str.replace(" ", "")
                child_str = child_str.replace("\n", "")
                if "<" in child_str:
                    continue
                if len(child_str) == 0:
                    continue
                if flg:
                    f = open(
                        f'C:\\Users\\70745\\PycharmProjects\\edgeTtsWrapper\\data'
                        f'\\次第花开-{"{:0>2d}".format(i-5)}-{child_str}.txt', mode='w', encoding='utf-8')
                    flg = False
                f.write(child)
                f.write('\n')
